#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMouseEvent>
#include <QPainter>
#include <QMessageBox>
//棋盘行和列
#define chess_row 20
#define chess_columes 20
//棋盘格的宽、高
#define width 25
#define height 25
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    resize(520,520);
    l_isblack = 0;//初始化落子颜色 0 为白色 1为黑色
    l_piece.clear();//清空我们的落子
}
void MainWindow::darw_chessborad(){

    QPainter painter(this);
    QPen pen;//画笔
    pen.setColor(Qt::black);//画笔颜色
    pen.setWidth(2);//设置画笔的宽度
    QBrush brush(QColor(Qt::darkGreen));//画刷并设置颜色
    painter.setPen(pen);//添加画笔
    painter.setBrush(brush);//添加画刷
  //使用for循环来绘制20*20个棋盘格子
    for(int j = 0;j<chess_columes;j++)
    {
        for(int i = 0;i<chess_row;i++)
        {

            //前两个参数代表矩形的左上顶点，后两个参数代表矩形的宽高
            painter.drawRect((i+0.5)*width,(j+0.5)*height,width,height);
        }
    }
}
void MainWindow::draw_piece(){
    QPainter painter(this);
    QPen pen;
    QBrush brush;
    pen.setColor(Qt::transparent);//设置画笔颜色为透明色
    painter.setPen(pen);
    //循环我们的所有落子，并绘制出来
    for(int i = 0;i<l_piece.size();i++)
    {
        piece item = l_piece[i];
        //判断是黑方还是白方落子
        if(item.m_bBlack)
        {
            painter.setBrush(Qt::black);
        }else {
            painter.setBrush(Qt::white);
        }
        //获取棋子的位置
        QPoint ptCenter((item.m_pt.x()+0.5)*width,(item.m_pt.y()+0.5)*height);
        //绘制棋子
painter.drawEllipse(ptCenter,height/2,width/2);}}


//获取一个方向的棋子数量
int MainWindow::CountRoundpiece(piece it, QPoint pt)
{
    piece it1 = it;//通过it1来获取当前棋子
    it1.m_pt += pt;//获取对应方向的棋子
    int counter = 0;
    while (l_piece.contains(it1)) {//判断it1是否在l_piece中
        counter++;//在这个方向的棋子数
        it1.m_pt += pt;
    }
    return counter;//返回棋子数量
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    QPoint pt;//定义一个点
    int chess_x = event->pos().x();//获取鼠标点击的x坐标
    int chess_y = event->pos().y();//获取鼠标点击的y坐标
    pt.setX(chess_x/width);//设置棋子x坐标
    pt.setY(chess_y/height);//设置棋子y坐标
    //循环所有棋子判断落子处是否存在棋子
    for(int i = 0;i<l_piece.size();i++){
      piece item = l_piece[i];
        if(item.m_pt == pt){
            return;
        }
    }
    piece item(pt,l_isblack);//设定棋子坐标及颜色
    l_piece.append(item);//添加棋子到所有的棋子
    //判定是否赢
    int Left = CountRoundpiece(item,QPoint(-1,0));//获取左边棋子的数量
    int Right = CountRoundpiece(item,QPoint(+1,0));
    int Up =CountRoundpiece(item,QPoint(0,-1));
    int Down = CountRoundpiece(item,QPoint(0,+1));
    int LeftUp = CountRoundpiece(item,QPoint(-1,-1));
    int LeftDown = CountRoundpiece(item,QPoint(-1,+1));
    int RightUp = CountRoundpiece(item,QPoint(+1,-1));
    int RightDown =CountRoundpiece(item,QPoint(+1,+1));
    //判定四种情况
    if((Left+Right >= 4)|| (Up+Down)>=4 || (LeftUp+RightDown)>=4||(RightUp + LeftDown>=4))
    {
        QString strTip = item.m_bBlack?"黑棋胜出！":"白棋胜出！";//判断当前落子的颜色给出相应的获胜提示
        QMessageBox::information(this,"提示胜利",strTip,QMessageBox::Yes);
        l_piece.clear();//清空所有落子
        return;
    }
    l_isblack = !l_isblack;//切换棋子颜色
}
void MainWindow::paintEvent(QPaintEvent *event)
{
    //绘制棋盘
    darw_chessborad();
    //绘制棋子
   draw_piece();
update();//界面刷新函数，用来刷新下棋后的界面

}

MainWindow::~MainWindow()
{
    delete ui;
}
